package hsb.ruoyi;

import com.fasterxml.jackson.databind.ObjectMapper;
import hsb.ruoyi.admin.orm.User;
import hsb.ruoyi.common.annotation.Log;
import hsb.ruoyi.common.constant.Constants;
import hsb.ruoyi.common.core.controller.BaseController;
import hsb.ruoyi.common.core.domain.R;
import hsb.ruoyi.common.core.domain.TokenService;
import hsb.ruoyi.common.core.domain.model.LoginUser;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 胡帅博
 * @date 2023/1/1 22:55
 */

@Path("test")

@Consumes(MediaType.APPLICATION_JSON)
public class TestController extends BaseController {

    //接收application/json类型数据

    @Inject
    TokenService tokenService;

    @POST
   // @Log(title = "登陆测试", operatorType = OperatorType.MANAGE)
    @Log(title = "登陆测试")
    @Path("/json")
    @Produces(MediaType.APPLICATION_JSON)
    public User jsonTest(User user) {
        return user;
    }

    @Inject
    ObjectMapper objectMapper;

    @GET
    @Path("/getpage")
   // @Log(title = "formDataTest", operatorType = OperatorType.MANAGE)
    // @Produces(MediaType.APPLICATION_FORM_URLENCODED)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Map getTest(MultivaluedMap<String, String> name) {
        // startPage();

        //  objectMapper.convertValue(name, User.class);

        return name;
    }

    //接收multipart/form-data类型数据
    @POST
    @Path("/form")
    // @Log(title = "formDataTest", operatorType = OperatorType.MANAGE)
    // @Produces(MediaType.APPLICATION_FORM_URLENCODED)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Map formDataTest(MultivaluedMap<String, String> name) {
        return name;
    }

    @POST
    @Path("/lists")
    // @Log(title = "formDataTest", operatorType = OperatorType.MANAGE)
    // @Produces(MediaType.APPLICATION_FORM_URLENCODED)
    // @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public List<Long> formDataTest(@MatrixParam("list") List<Long> list) {
        return list;
    }


//    @POST
//    @Path("/login")
//    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
//    public R<Map> login(@QueryParam("openId") String openId) {
//        System.out.println(openId);
//        String token = IdUtils.fastUUID();
//        String jwt = JsonWebTokenUtil.issueJwt(token, openId,
//                "token-server", 3000L, List.of("appUser")
//        );
//        Map<String, String> body = new HashMap<>();
//        body.put("token", jwt);
//        return R.ok(body);
//    }

    @POST
 //   @Log(title = "登陆测试", operatorType = OperatorType.MANAGE)
    @Path("/login2")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public R<Map> login2(@FormParam("openId") String openId) {
        LoginUser loginUser = new LoginUser();
        loginUser.setTypePrefix("app");
        loginUser.setUserId(Long.parseLong(openId));
      //  loginUser.setRoles(List.of("appUser"));
        String token = tokenService.createToken(loginUser);
        Map<String, String> body = new HashMap<>();
        body.put(Constants.TOKEN, token);
        return R.ok(body);
    }


//    @POST
//    @Path("/form2/{user}")
//    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
//    public int formDataTest2(User user) {
//        System.out.println(user);
//        return 1;
//    }

}
